package com.marosoft;
/**
 * @author aizaz.haider
 */
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

import com.marosoft.MapLegendHelper.LegendColor;

import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.Bitmap.CompressFormat;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Matrix;
import android.graphics.drawable.BitmapDrawable;
import android.os.Bundle;
import android.os.Environment;
import android.util.Log;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.MeasureSpec;
import android.view.ViewGroup.LayoutParams;
import android.widget.Button;
import android.widget.RelativeLayout;
import android.widget.Toast;

public class MergeImages extends Activity {
	protected static final String TAG = MergeImages.class.getName();
	Bitmap mBackImage, mTopImage, mBackground, mLogoImage, mNewSaving;
	Canvas mComboImage;
	FileOutputStream mFileOutputStream;
	BitmapDrawable mBitmapDrawable;
	private static String mTempDir;
	private String mCurrent = null;
	private Button mSave = null;

	private final int WIDTH = 800;
	private final int HEIGHT = 800;
	private final int WIDTH_LEGEND = 150;
	private final int HEIGHT_LEGEND = 350;
	private final float PADDING_TOP = 10F;
	private final float PADDING_RIGHT = 10F;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);

		mTempDir = Environment.getExternalStorageDirectory() + "/" + "Image-Merger-AZ" + "/";
		mCurrent = "Map.png";
		prepareDirectory();

		mBackground = Bitmap.createBitmap(WIDTH, HEIGHT, Bitmap.Config.ARGB_8888);
		mBackImage = BitmapFactory.decodeResource(getResources(), R.drawable.map); // map
		// mTopImage = BitmapFactory.decodeResource(getResources(), R.drawable.legend);

//		LayoutInflater inflater = (LayoutInflater) this.getSystemService(LAYOUT_INFLATER_SERVICE);
//		RelativeLayout view = (RelativeLayout) inflater.inflate(R.layout.map_legend, null);

		MapLegendHelper helper = new MapLegendHelper(this);
//		helper.setIv1(LegendColor.RED);
//		helper.setTv1("changed");
		RelativeLayout view = helper.getView();
		
		mTopImage = getBitmapFromView(view);

		mTopImage = Bitmap.createScaledBitmap(mTopImage, WIDTH_LEGEND, HEIGHT_LEGEND, true);
		System.gc();

		mLogoImage = BitmapFactory.decodeResource(getResources(), R.drawable.logo); 

		mComboImage = new Canvas(mBackground);
		mComboImage.drawBitmap(mBackImage, 50f, 10f, null);
		mComboImage.drawBitmap(mTopImage, WIDTH - (mTopImage.getWidth() + PADDING_RIGHT), PADDING_TOP, null);
		mComboImage.drawBitmap(mLogoImage, WIDTH - (mLogoImage.getWidth() + PADDING_RIGHT), mTopImage.getHeight() + PADDING_TOP, null);
		mFileOutputStream = null;

		mSave = (Button) findViewById(R.id.btn_save);
		mSave.setOnClickListener(new View.OnClickListener() {
			public void onClick(View v) {
				Log.v(TAG, "Save Tab Clicked");
				try {
					mBitmapDrawable = new BitmapDrawable(mBackground);
					mNewSaving = ((BitmapDrawable) mBitmapDrawable).getBitmap();
					String FtoSave = mTempDir + mCurrent;
					File mFile = new File(FtoSave);
					mFileOutputStream = new FileOutputStream(mFile);
					mNewSaving.compress(CompressFormat.PNG, 95, mFileOutputStream);
					mFileOutputStream.flush();
					mFileOutputStream.close();
				} catch (FileNotFoundException e) {
					Log.v(TAG, "FileNotFoundExceptionError " + e.toString());
				} catch (IOException e) {
					Log.v(TAG, "IOExceptionError " + e.toString());
				}
			}
		});

	}// onCreate

	private boolean prepareDirectory() {
		try {
			if (makeDirectory()) {
				return true;
			} else {
				return false;
			}
		} catch (Exception e) {
			e.printStackTrace();
			Toast.makeText(this, "SD Card Error", 1000).show();
			return false;
		}
	}

	private boolean makeDirectory() {
		File mTempFile = new File(mTempDir);
		if (!mTempFile.exists()) {
			mTempFile.mkdirs();
		}

		if (mTempFile.isDirectory()) {
			File[] mFiles = mTempFile.listFiles();
			for (File mEveryFile : mFiles) {
				if (!mEveryFile.delete()) {
					System.out.println("Failed to delete" + mEveryFile);
				}
			}
		}
		return (mTempFile.isDirectory());
	}

	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		if ((!(android.os.Build.VERSION.SDK_INT > android.os.Build.VERSION_CODES.DONUT) && keyCode == KeyEvent.KEYCODE_BACK && event.getRepeatCount() == 0)) {
			onBackPressed();
		}
		return super.onKeyDown(keyCode, event);
	}

	public void onBackPressed() {
		finish();
	}

	public Bitmap getBitmapFromView(ViewGroup v) {
		v.setLayoutParams(new LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT));
		v.measure(MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED), MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED));
		v.layout(0, 0, v.getMeasuredWidth(), v.getMeasuredHeight());
		Bitmap b = Bitmap.createBitmap(v.getMeasuredWidth(), v.getMeasuredHeight(), Bitmap.Config.ARGB_8888);

		Canvas c = new Canvas(b);
		v.draw(c);
		return b;
	}
}